import axios from 'axios'
import apiNo from '@/config/api'
import store from '@/store'
const instance = axios.create({
  baseURL: apiNo.baseURL || window.baseURL,
  timeout: 10 * 1000,
  responseType: 'json',
  headers: { 'content-type': 'application/x-www-form-urlencoded' }
})
// 添加响应拦截器
instance.interceptors.response.use(res => {
  console.log(res)
  let { data } = res
  return data
}, error => {
  store.dispatch('toast', '数据获取失败，请稍后再试')
  return error
})

async function post (type, params, options) {
  if (!type) { throw new Error({ msg: '接口类型不可遗漏' }) }
  let res = await instance.post(`${apiNo[type]}/`, params, options)
  return res
}

class Api {
  // 关键字查询
  11 (key_word = '', page = 1, length = 10) {
    return post(11, { key_word, page, length }
    )
  }
  12 () { return post(12) }
  13 (title = '') { return post(13, { title }) }
  // 知识问答
  21 (question = '', entity_count = 3) {
    return post(21, { question, entity_count })
  }
  22 () { return post(22) }
  23 (entity = '') { return post(23, { entity }) }
  // 个性化培训
  31 (domainname = '') { return post(31, { domainname }) }
  32 (contentname = '') { return post(32, { contentname }) }
  33 () { return post(33) }
  34 (domain = '', index = '') { return post(34, { domain, index }) }
  35 (domain = '', index = '', path = '', path_id = '') { return post(35, { domain, index, path, path_id }) }
  36 () { return post(36) }
  // 知识卡片
  41 (noun = '') { return post(41, { noun }) }
  42 () { return post(42) }
  43 () { return post(43) }
  44 (query = '', category = '') { return post(44, { query, category }) }
  // 安全体检页
  52 (domain = '') { return post(52, { domain }) }
  53 (standard = '') { return post(53, { standard }) }
  54 (params = {}) { return post(54, params) }
}
const api = new Api()
export default api
